package com.aliya.glide

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.aliya.glide.databinding.ActivityMainBinding
import com.aliya.glide.ktx.viewBindings
import com.aliya.glide.module.GlideHelper


class MainActivity : AppCompatActivity() {

    companion object {
        const val TAG = "MainActivity"
    }

    private val viewBindings by viewBindings(ActivityMainBinding::bind)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewBindings.tvList.setOnClickListener {
            startActivity(Intent(it.context, ListActivity::class.java))
        }
        viewBindings.tvTransform.setOnClickListener {
            startActivity(Intent(it.context, TransformActivity::class.java))
        }
        viewBindings.tvTarget.setOnClickListener {
            startActivity(Intent(it.context, CustomTargetActivity::class.java))
        }
        viewBindings.tvTransition.setOnClickListener {
            startActivity(Intent(it.context, TransitionsActivity::class.java))
        }

        viewBindings.tvLifecycle.setOnClickListener {
//            startActivity(Intent(it.context, LifecyclesActivity::class.java))
            var url =
                "https://up.enterdesk.com/edpic_source/53/0a/da/530adad966630fce548cd408237ff200.jpg"
//            var url = "https://lmg.jj20.com/up/allimg/4k/s/02/21092423260Q119-0-lp.jpg"
            Thread {
//                val requestOptions = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC)
//                    .priority(Priority.HIGH)
//                    .skipMemoryCache(false)
//                val startMs = SystemClock.uptimeMillis()
//                val submit = Glide.with(it.context).`as`(File::class.java).apply(requestOptions).load(url).submit()
////                val submit = Glide.with(it.context).downloadOnly().load(url).submit()
//                Log.e("TAG", "submit: " + (SystemClock.uptimeMillis() - startMs))
//                val get = submit.get()
//                Log.e("TAG", "submit: " + (SystemClock.uptimeMillis() - startMs) + " " + get)
                val inputStream = GlideHelper.getResourceInputStream(url)
                Log.e("a_liYa", "inputStream $inputStream")
                if (inputStream != null) {
                    val byteArray = ByteArray(300 * 1024)
                    var len = 0
                    var contentLen = 0L
                    while (inputStream.read(byteArray).let {
                            len = it
                            it != -1
                        }) {
                        contentLen += len
                    }
                    Log.e("a_liYa", "读取结束: $contentLen")
                }

            }.start()
        }
    }
}